package com.csi.jf.crucible.ptservice.service.impl;


import java.util.List;

import com.chinasofti.ro.bizframework.core.orm.IDAO;
import com.chinasofti.ro.bizframework.core.orm.Page;
import com.chinasofti.ro.bizframework.core.orm.entity.Model;
import com.csi.jf.crucible.common.config.JfConstants;
import com.csi.jf.crucible.ptservice.model.PTSTask;
import com.csi.jf.crucible.ptservice.model.ServiceOrders;
import com.csi.jf.crucible.ptservice.service.ServiceOrdersService;

/**
 * @Title: ServiceOrdersServiceImpl.java
 * @Description: <br>
 *               <br>
 * @Company: ICSS
 * @Created on 2014-7-7 下午9:59:49
 * @author FengHaiBing
 */
public class ServiceOrdersServiceImpl implements ServiceOrdersService {

	private IDAO dao;
	
	public void setDao(IDAO dao) {
		this.dao = dao;
	}

	/**
	 * 根据id查询数据
	 * 
	 * @param id
	 * @return Model
	 */
	public Model findById(Long id) {
		ServiceOrders pTSservice = new ServiceOrders();
		pTSservice.setId(id);
		return (Model) dao.createQuery(pTSservice).first();
	}
	
	/**
	 * 分页查询
	 * 
	 * @param serviceOrders
	 * @param page 分页
	 * @return
	 */
	public Page findByPage(ServiceOrders serviceOrders, Page page, String projectID) {
		String sql = "SELECT ID,CODE_PACKAGES_ID,PROJECT_NAME,SERVICE_ID,SERVICE_NAME,APPLY_DATE,PROFESSOR_ID,PROFESSOR_NAME,EXE_BEGIN_DATE,EXE_END_DATE,SERVICE_PRICE,REVIEW_ID,REVIEW_NAME,STATUS FROM jf_pts_service_orders WHERE 1=1 and PROJECT_ID="+projectID;//使用pTSservice.hbm.xml中的id为list的sql查询
		return dao.createNamedQuery(serviceOrders.getClass(),sql, serviceOrders).fetch(page);
	}
	
	/**
	 * 批量删除数据
	 * 
	 * @param ids
	 * @return int[] delete records
	 */
	public int[] batchDelete(String ids) {
		ServiceOrders pTSservice = new ServiceOrders();
		return dao.batchDelete(pTSservice, ids);
	}

	/**
	 * 保存数据
	 * @param Model
	 */
	public void save(Model model) {
		ServiceOrders ps = (ServiceOrders)model;
		//同时保存任务信息
		PTSTask task = new PTSTask();
		task.setTaskName(ps.getServiceName());
		task.setPrice("1000");
		task.setCreator(ps.getCreator());
		task.setStatus(ps.getStatus());
		task.setPersonId(ps.getProfessorID());//专家ID
		task.setProjectId(ps.getProjectId());
		dao.save(task);
		
		ps.setTaskId(task.getTaskId());
		dao.save(ps);
		
	}

	/**
	 * 更新数据
	 * @param Model
	 */
	public void update(Model model) {
		dao.update(model);
	}

    @Override
    public Page findByPage(ServiceOrders serviceOrders, Page page) {
        String sql = "SELECT ID,TASK_ID,CODE_PACKAGES_ID,PROJECT_ID,project_Name,SERVICE_ID,SERVICE_NAME,APPLY_DATE,PROFESSOR_ID,PROFESSOR_NAME,EXE_BEGIN_DATE,EXE_END_DATE,SERVICE_PRICE,STATUS " +
        		"FROM jf_pts_service_orders " +
        		"WHERE 1=1 and STATUS='"+JfConstants.PTS_SERVICE_CHECK+"'";//使用pTSservice.hbm.xml中的id为list的sql查询
        return dao.createNamedQuery(serviceOrders.getClass(),sql, serviceOrders).fetch(page);
    }

    @Override
    public Page findDoneOrdersByPage(ServiceOrders serviceOrders, Page page) {
        String sql = "SELECT ID,TASK_ID,CODE_PACKAGES_ID,PROJECT_ID,project_Name,SERVICE_ID,SERVICE_NAME,APPLY_DATE,PROFESSOR_ID,PROFESSOR_NAME,EXE_BEGIN_DATE,EXE_END_DATE,SERVICE_PRICE,STATUS " +
        		"FROM jf_pts_service_orders " +
        		"WHERE 1=1 and STATUS<>'"+JfConstants.PTS_SERVICE_CHECK+"'";//使用pTSservice.hbm.xml中的id为list的sql查询
        return dao.createNamedQuery(serviceOrders.getClass(),sql, serviceOrders).fetch(page);
    }

    @Override
    public void allocateProfessor(Long orderId) {
        String sql = "Update jf_pts_service_orders set PROFESSOR_ID=?,status='1' where id=?";//使用pTSservice.hbm.xml中的id为list的sql查询
        int result=  dao.execute(sql, new Long[]{4L,orderId});
        
    }
	@Override
	public List<ServiceOrders> getAllServiceOrders() {
		
		ServiceOrders serviceOrders=new ServiceOrders();
		StringBuffer sql=new StringBuffer();
		sql.append("select * from jf_pts_service_orders order by id desc");
		@SuppressWarnings("unchecked")
		List<ServiceOrders> ordersList = dao.createNamedQuery(ServiceOrders.class, sql.toString(), serviceOrders).list();
		return ordersList;
		
	}

	@Override
	public String getReviewIdByOrId(Long orderId) {
		ServiceOrders serviceOrders=new ServiceOrders();
		serviceOrders.setId(orderId);
		serviceOrders= (ServiceOrders)dao.createQuery(serviceOrders).first();
		return serviceOrders.getReviewId();
	}

	@Override
	public Model findByTaskId(Long taskId) {
		ServiceOrders pTSservice = new ServiceOrders();
		pTSservice.setTaskId(taskId);
		return (Model) dao.createQuery(pTSservice).first();
	}
	
}